<?php
class Home extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('model_home');
	}
	
	function index(){
		$data['title'] = TITLE.' - Home';
		/*ok
		if($this->input->post("btnregister") && (!Auth::isLoggedIn()) ){
			$redirect = $this->_register();
			if($redirect) $data['register_success'] = "y";
			$this->form_validation->_field_data = array();
		}*/
		
		$this->load->view("index",$data);
		
		
	}
	
	function denied(){
			$data['title'] = TITLE.' - Access Denied';
			$this->load->view("denied");
	}
	function hello(){
		
			 
		$this->cezpdf->ezText('Hello World', 12, array('justification' => 'center'));
		$this->cezpdf->ezSetDy(-10);

		$content = 'The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.
					Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.';

		$this->cezpdf->ezText($content, 10);

		$this->cezpdf->ezStream();
		
	
	}
	
	function polling(){
		$polling = $this->input->post("polling",true);
		$ip = $this->input->ip_address();
		if($this->model_home->is_unique_ip($ip)){
			$this->model_home->addPolling($polling,$ip);
			echo "Thank you for filling this polling form. Feel free to explore our website.";
		}else{
			echo "Sorry, you have filled this polling with the same ip address before";
		}
	}
	
	function error404(){
		$data['title'] = TITLE.' - Page not found';
		$this->load->view("general/error404",$data);
	}
	
	function about(){
		$data['aboutUs'] = $this->model_home->getSystemVariable("about_us");
		$data['title'] = TITLE.' - About Us';
		$this->load->view("about",$data);
	}
	function privacy(){
		$data['privacy'] = $this->model_home->getSystemVariable("privacy"); 
		$data['title'] = TITLE.' - Privacy';
		$this->load->view("privacy",$data);
	}
	function terms(){
		$data['terms_and_condition'] = $this->model_home->getSystemVariable("terms_and_condition"); 
		$data['title'] = TITLE.' - Terms';
		$this->load->view("terms",$data);
	}
	function faq(){
		$data['faq'] = $this->model_home->getFaq();
		$data['javascript'][] = "faq.js";
		$data['title'] = TITLE.' - FAQ';
		$this->load->view("faq",$data);
	}
	function contactus(){
		$data['contact'] = $this->model_home->getSystemVariable("contact");
		$data['active_contact'] = 'y';
		$data['title'] = TITLE.' - Contact Us';
		$this->load->view("contactus",$data);
	}
	
	function login(){
		if(Auth::isLoggedIn()) redirect("/");
		if( Auth::login($this->input->post('username',true), $this->input->post('password',true)) ){
			redirect("/");
		}else{
			redirect("/");
		}
	}
	
	function logout(){
		Auth::logout();
	}
	
	function forgot_password(){
		$this->form_validation->set_error_delimiters(' ', ' ');
		$data['title'] = TITLE.' - Forgot Password';
		if($this->input->post("btnSubmit")){
			$this->form_validation->set_message('_checkemailforgot', 'Email not found.');
			$config = array(
				array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email|callback__checkemailforgot|xss_clean'
                  )
				);
			$this->form_validation->set_rules($config); 
			if ($this->form_validation->run() == FALSE){
			}
			else{
				//save to database..
				$this->load->model("model_user");
				$secret_key = $this->model_user->forgot_password();
				
				//send email
				//unfinished
				
				$data['pesansukses'] = 'Link to reset your password has been sent to your email.';
			}
			
		}
		
		
		$this->load->view("forgot_password",$data);
	}
	
	function reset_password(){
		$data['title'] = TITLE.' - Reset Password';
		$this->load->view("reset_password",$data);
	}
	
	function reset_success(){
		$data['title'] = TITLE.' - Reset Password Success';
		$data['success'] = 'y';
		$this->load->view("reset_password",$data);
	}
	

	function _register(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|callback__checkusername|xss_clean'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'name',
                     'label'   => 'Name',
                     'rules'   => 'trim|required|xss_clean'
                  ),   
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email|callback__checkemail|xss_clean'
                  ),
			   array(
                     'field'   => 'phone',
                     'label'   => 'Phone',
                     'rules'   => 'trim|required|is_natural|xss_clean'
                  ),
			   array(
                     'field'   => 'address',
                     'label'   => 'Address',
                     'rules'   => 'trim|required|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->load->model("model_user");
			$this->model_user->register();
			return true;
		}
	}
	function _checkusername($str){
		$this->form_validation->set_message('_checkusername', 'Username already exists');
		$this->load->model("model_user");
		$unique = $this->model_user->checkunique("username",$str);
		return $unique;
	}
	 
}